<?php

namespace common\modules\wechat\models\backend;

use common\widgets\grid\CheckboxColumn;
use common\widgets\GridView;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * ModuleSearch represents the model behind the search form about `common\modules\wechat\models\backend\Module`.
 */
class ModuleSearch extends Module
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'name', 'type', 'category', 'version', 'ability', 'description', 'author', 'site'], 'safe'],
            [['admin', 'migration', 'reply_rule', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/wechat', 'Module');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Module::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'admin'      => $this->admin,
            'migration'  => $this->migration,
            'reply_rule' => $this->reply_rule,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'created_by' => $this->created_by,
            'updated_by' => $this->updated_by,
        ]);

        $query->andFilterWhere(['like', 'id', $this->id])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'type', $this->type])
            ->andFilterWhere(['like', 'category', $this->category])
            ->andFilterWhere(['like', 'version', $this->version])
            ->andFilterWhere(['like', 'ability', $this->ability])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'author', $this->author])
            ->andFilterWhere(['like', 'site', $this->site]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [
            [
                'class' => CheckboxColumn::classname(),
            ],

            [
                'attribute' => 'id',
            ],

            'name',

            'type',

            'category',

            'version',

            'ability',

            'description',

            'author',

            'site',

            'admin',

            'migration',

            'reply_rule',

            [
                'attribute' => 'created_at',
                'format'    => ['date', 'php:Y-m-d H:i:s'],
                'filterType' => GridView::FILTER_DATETIME,
            ],

            [
                'attribute' => 'updated_at',
                'format'    => ['date', 'php:Y-m-d H:i:s'],
                'filterType' => GridView::FILTER_DATETIME,
            ],

            [
                'class' => 'common\widgets\grid\ActionColumn',
            ],
        ];
    }
}
